package pers.cz.job;

import pers.cz.mapper.AutoCollectMethodParamsMapper;
import pers.cz.mapper.AutoCollectTestCaseMapper;
import pers.cz.mapper.TestCaseMapper;
import pers.cz.model.AutoCollectTestCaseWithBLOBs;
import pers.cz.model.TestCase;
import pers.cz.spring.SpringContextUtil;
import pers.cz.thread.IThreadGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * 处理清洗测试案例，主要用于删除录制表中的数据。
 * @program: PostGirl-panent
 * @description: DeleteAutoTestCaseThread
 * @author: Cheng Zhi
 * @create: 2022-03-18 17:01
 **/
public class DeleteAutoTestCaseThread implements IThreadGenerator {

    Logger logger = LoggerFactory.getLogger(DeleteAutoTestCaseThread.class);

    String traceId;  // 定义traceId
    String operType; // 定义操作类型 0表示删除auto_collect_test_case和auto_collect_method_params 1表示只删除auto_collect_test_case
    public DeleteAutoTestCaseThread(String traceId, String operType) {

        this.traceId = traceId;
        this.operType = operType;
    }

    @Override
    public void run() {

        if (operType == "0") {
            deleteAutoCollectTestCase(traceId);
            deleteAutoCollectMethodPrams(traceId);
        } else if (operType == "1") {
            // 先把数据搬迁到测试案例表中，然后删除录制表。
            moveData(traceId);
        }
    }

    /**
     * 数据搬迁，将录制的案例沉淀到自动化案例中去
     * @param traceId
     */
    @Transactional(rollbackFor = Exception.class)
    public void moveData(String traceId) {

        // 手动开启事务
        DataSourceTransactionManager bean = SpringContextUtil.getBean(DataSourceTransactionManager.class);
        //2.获取事务定义
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        //3.设置事务隔离级别，开启新事务
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        TransactionStatus transaction = bean.getTransaction(def);
        try {
            // 先查询。
            AutoCollectTestCaseWithBLOBs autoCollectTestCaseWithBLOBs = SpringContextUtil.getBean(AutoCollectTestCaseMapper.class).selectByTraceId(traceId);

            // 然后插入到案例表
            TestCase testCase = new TestCase();
            testCase.setCaseNameZh("自动生成案例");
            testCase.setCaseName("Auto_Collect_TestCase");
            testCase.setBusiCode(autoCollectTestCaseWithBLOBs.getBusiCode());
            testCase.setRequest(autoCollectTestCaseWithBLOBs.getRequest());
            testCase.setExpectedResponse(autoCollectTestCaseWithBLOBs.getResponse());
            testCase.setCreateDate(autoCollectTestCaseWithBLOBs.getSyncTime());
            testCase.setLastUpdateDate(autoCollectTestCaseWithBLOBs.getSyncTime());
            testCase.setIgnoreFields("errorMsg");
            SpringContextUtil.getBean(TestCaseMapper.class).insertSelective(testCase);

            // 最后删除录制表
            deleteAutoCollectTestCase(autoCollectTestCaseWithBLOBs.getExt2());

            bean.commit(transaction);
        } catch (Exception e) {
            logger.error("操作数据库失败：开始回滚事务....");
            bean.rollback(transaction);
        }
    }
    /**
     * 删除录制接口出入参表。
     * @param traceId
     */
    private void deleteAutoCollectTestCase(String traceId) {

        SpringContextUtil.getBean(AutoCollectTestCaseMapper.class).deleteByTraceId(traceId);
    }

    /**
     * 删除录制底层方法出入参表。
     * @param traceId
     */
    private void deleteAutoCollectMethodPrams(String traceId) {
        SpringContextUtil.getBean(AutoCollectMethodParamsMapper.class).deleteByTraceId(traceId);
    }
}
